//
//  JobViewController.m
//  intelligent
//
//  Created by 甘世清 on 2024/2/5.
//

#import "JobViewController.h"
#import "JobCell.h"
#import "JobDetailsViewController.h"
#import "JobModel.h"
#import "JobHeaderView.h"
@interface JobViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@end

@implementation JobViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
}
- (void)SQ_Header{
    [HttpTool post:CZLB paramDic:[NSMutableDictionary dictionary] successBlock:^(BaseModel * _Nonnull httpModel) {
        [self.jsonArray removeAllObjects];
        self.jsonArray = [CreateModel mj_objectArrayWithKeyValuesArray:httpModel.data];
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
        [self.collectionView.mj_header endRefreshing];
    }];
}
- (void)SQ_Foother{
    [self.collectionView.mj_footer endRefreshing];
}
 
- (void)SQ_UIViewForSelfView{
    UIImageView * bgImage = [[UIImageView alloc]init];
    bgImage.image = SQGetImage(@"背景");
    bgImage.clipsToBounds = YES;
    bgImage.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bgImage];
    [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(SCREEN_WIDETH*184/375);
    }];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.zx_navBar.mas_bottom);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
    [self.collectionView registerNib:[UINib nibWithNibName:@"JobCell" bundle:nil] forCellWithReuseIdentifier:@"JobCell"];
    
    [self.collectionView registerClass:[JobHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JobHeaderView"];
    [self SQ_collectViewAddRefresh];
    [self SQ_Header];
}

// 设置区头高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(collectionView.frame.size.width, 30); // 例如设置高度为30点
}

// 返回区头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        CreateModel * model = self.jsonArray[indexPath.section];
        JobHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JobHeaderView" forIndexPath:indexPath];
        // 设置区头视图的内容
        headerView.headerTitleLabel.text = model.name;
        return headerView;
    }
    return nil;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.jsonArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    CreateModel * model = self.jsonArray[section];
    return model.works.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    JobCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JobCell" forIndexPath:indexPath];
    CreateModel * model = self.jsonArray[indexPath.section];
    cell.model = model.works[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CreateModel * createmodel = self.jsonArray[indexPath.section];
    JobModel * model = createmodel.works[indexPath.row];
    JobDetailsViewController * vc = [JobDetailsViewController new];
    vc.worksId = [NSString stringWithFormat:@"%ld",model.jobId];
    vc.title = model.name;
    vc.placeholder = model.remark;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat itemW = (SCREEN_WIDETH - 63) / 2;
    CGFloat itemH = itemW / 156 * 80;
    return CGSizeMake(itemW, itemH);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 24.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 15.0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(24, 24, 24, 24);
}

@end
